#include<bits/stdc++.h>
#define ll long long
#define linf 0x3f3f3f3f3f3f3f3f
#define inf 0x7fffffff
#define INF 0x3f
#define v e[i].y
using namespace std;
inline ll read() {
    char ch = getchar();ll x = 0, w = 1;
    while (ch < '0' || ch>'9') { if (ch == '-')w = -1;ch = getchar(); }
    while (ch >= '0' && ch <= '9')x = (x << 1) + (x << 3) + ch - 48, ch = getchar();return w == 1 ? x : -x;
}
inline void write(ll x) {
    if (x < 0)x = -x, putchar('-');
    if (x < 10) { putchar(48 + x);return; }
    write(x / 10), putchar((x + 10) % 10 + 48);
}
int n = read(), m = read(), q[100005], ans, h, t, maxl;
bool dp[2000005];
char xk[2000005];
struct AC {
    int ch[30005][30], Fail[30005], val[30005], num;
    void Insert(int L) {
        int op = 0;
        for (int i = 1, j;i <= L;i++) {
            j = xk[i] - 'a';
            op = (ch[op][j] ? ch[op][j] : ch[op][j] = ++num);
        }
        val[op] = L, maxl = max(maxl, L);
    }
    void getfail() {
        h = 0, t = 0;
        for (int i = 0;i < 26;i++)if (ch[0][i])q[++t] = ch[0][i], Fail[ch[0][i]] = 0;
        while (h < t) {
            int u = q[++h];
            for (int i = 0;i < 26;i++) {
                if (ch[u][i])Fail[ch[u][i]] = ch[Fail[u]][i], q[++t] = ch[u][i];
                else ch[u][i] = ch[Fail[u]][i];
            }
        }
    }
    int solve(int L) {
        for (int i = 1;i <= L;i++)dp[i] = 0;
        dp[0] = 1, ans = 0;
        int op = 0;
        for (int i = 1, j;i <= L;i++) {
            if (ans + maxl < i)break;
            j = xk[i] - 'a', op = ch[op][j];
            for (int k = op;k;k = Fail[k]) {
                dp[i] |= dp[i - val[k]];
                if (dp[i])break;
            }
            if (dp[i])ans = i;
        }
        return ans;
    }
}A;
int main() {
    while (n--)scanf("%s", xk + 1), A.Insert(strlen(xk + 1));
    A.getfail();
    while (m--)scanf("%s", xk + 1), write(A.solve(strlen(xk + 1))), putchar(10);
    return 0;
}